package com.scs.application.core.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 树形节点
 */
@Data
@JsonPropertyOrder(value = {"id", "title", "leaf", "parentId", "properties", "children"})
public class TreeNode implements Serializable {

    @JSONField(ordinal = 1, name = "id")
    private String id;

    @JSONField(ordinal = 2, name = "title")
    private String title;

    @JSONField(ordinal = 3, name = "leaf")
    private boolean leaf;

    @JSONField(ordinal = 4, name = "parentId")
    private String parentId;

    @JSONField(ordinal = 5, name = "properties")
    private Map<String, Object> properties;

    @JSONField(ordinal = 6, name = "children")
    private List<TreeNode> children;

    public void addProperty(String key, Object value) {
        addProperty(true, key, value);

    }

    public void addProperty(boolean condition, String key, Object value) {
        if (!condition) {
            return;
        }
        if (properties == null) {
            properties = new LinkedHashMap<>();
        }
        properties.put(key, value);

    }
}
